package com.wjk.blogs.web.admin;

import com.wjk.blogs.po.Type;
import com.wjk.blogs.service.BlogService;
import com.wjk.blogs.service.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.validation.Valid;


@Controller
@RequestMapping("/admin")
public class TypeController {
    @Autowired
    private BlogService blogService;

    @Autowired
    private TypeService typeService;

    @GetMapping("/types")
    public String types(//页面数据显示
            @PageableDefault(
                    size= 10
                    ,sort={"id"}
                    ,direction = Sort.Direction.DESC
            ) Pageable pageable
            , Model model){

        /*model,前端页面栈时模型*/

        model.addAttribute("page"
                ,typeService.listType(pageable)
        );
        return "admin/types";
    }

    @GetMapping("/types/input")
    public String input(Model model){//添加
        model.addAttribute("type",new Type());
        return "admin/types-input";
    }

    @GetMapping("/types/{id}/input")
    public String editInput(@PathVariable Long id, Model model){//编辑时按id找数据
        model.addAttribute("type",typeService.getType(id));
        return "admin/types-input";
    }

    @PostMapping("/types")
    //@Valid和BindingResult配套使用，@Valid用在参数前，BindingResult作为校验结果绑定返回
    public String post(@Valid Type type
            , BindingResult result
            ,RedirectAttributes attributes){

        //添加或者编辑时确保数据库中没有重复数据
        Type type1=typeService.getTypeByName(type.getName());
        if (type1!=null){
            result.rejectValue("name"
                    ,"nameError"
                    ,"不能添加重复的分类");
        }

        //校验是否通过,如果通过了
        if (result.hasErrors()){
            return "admin/types-input";
        }


        //向数据库中保存数据
        Type t=typeService.saveType(type);
        if (t == null){
            //提示保存失败
            attributes.addFlashAttribute("message","操作失败");
        }else{
            //提示保存成功
            attributes.addFlashAttribute("message","操作成功");
        }

        return "redirect:/admin/types";
    }

    @PostMapping("/types/{id}")//更新
    public String editPost(
            @Valid Type type
            , BindingResult result
            ,@PathVariable Long id
            , RedirectAttributes attributes){


        Type type1=typeService.getTypeByName(type.getName());


        if (type1 != null){
            result.rejectValue("name","nameError","不能添加重复的分类");
        }

        if (result.hasErrors()){
            return "admin/types-input";
        }

        Type t=typeService.updateType(id, type);
        if (t == null){
            //提示保存失败
            attributes.addFlashAttribute("message","操作失败");
        }else{
            //提示保存成功
            attributes.addFlashAttribute("message","操作成功");
        }

        return "redirect:/admin/types";
    }


    @GetMapping("/types/{id}/delete")//删除标签
    public String delete(@PathVariable Long id,RedirectAttributes attributes){
        if (blogService.BlogHasType().contains(typeService.getType(id).getId().toString())){
            attributes.addFlashAttribute("message","删除失败,还有绑定此标签的博客,如要删除此标签,请在博客中取消对标签的绑定");
            return "redirect:/admin/types";
        }
        typeService.deleteType(id);
        attributes.addFlashAttribute("message","删除成功");
        return "redirect:/admin/types";
    }

}
